package com.abel.bigwater.data.jnrq

import com.abel.bigwater.data.BcdString
import io.netty.buffer.ByteBuf

class FLowGaugeData : JnrqReadData() {

    init {
        dataLen = 60
    }

    override fun computeDataLen(): Int {
        return dataLen!!
    }

    override fun fillBuffer(buf: ByteBuf) {
        assert(meterAddrStr?.size == 8)
        assert(sampleTimeStr?.size == 8)
        assert(readStatusStr?.size == 2)
        assert(reserved?.size == 2)

        val tmp = buf.alloc().buffer()
        tmp.writeBytes(meterAddrStr!!.hexArray)
        tmp.writeBytes(sampleTimeStr!!.hexArray)

        tmp.writeIntLE(flowDigitsInt!!.toInt())
        tmp.writeIntLE(waterDigitsInt!!.toInt())
        tmp.writeIntLE(revertDigitsInt!!.toInt())

        tmp.writeBytes(ByteArray(12) { _ -> 0 })

        tmp.writeIntLE(pressureInt!!.toInt())
        tmp.writeIntLE(speedInt!!.toInt())
        tmp.writeIntLE(timeSum!!.toInt())
        tmp.writeIntLE(realSumInt!!.toInt())

        tmp.writeBytes(readStatusStr!!.hexArray)
        tmp.writeBytes(reserved!!.hexArray)
    }

    override fun parseBuffer(buf: ByteBuf) {
        val tmp = buf.duplicate()
        meterAddrStr = BcdString.create(tmp, 8)
        sampleTimeStr = BcdString.create(tmp, 8)

        flowDigitsInt = tmp.readUnsignedIntLE()
        waterDigitsInt = tmp.readUnsignedIntLE()
        revertDigitsInt = tmp.readUnsignedIntLE()

        tmp.skipBytes(4 * 3)

        pressureInt = tmp.readUnsignedIntLE()
        speedInt = tmp.readUnsignedIntLE()
        timeSum = tmp.readUnsignedIntLE()
        realSumInt = tmp.readUnsignedIntLE()

        readStatusStr = BcdString.create(tmp, 2)
        reserved = BcdString.create(tmp, 2)

        meterAddr
        sampleTime
        waterDigits

        buf.skipBytes(dataLen!!)
    }

    /**
     * 瞬时
     */
    var flowDigitsInt: Long? = null

    var flowDigits: Double? = null
        get() = if (flowDigitsInt == null) null else flowDigitsInt!! / 100.0
        set(value) {
            field = value
            flowDigitsInt = if (value == null) null else (value * 100.0).toLong()
        }

    /**
     * 负向累计 4 D0 D1 D2 D3 BCD 码 D0 小数位，低位到高位
     */
    var revertDigitsInt: Long? = null

    var revertDigits: Double? = null
        get() {
            field = if (revertDigitsInt == null) null else revertDigitsInt!! / 100.0
            return field
        }
        set(value) {
            field = value
            revertDigitsInt = if (value == null) null else (value * 100.0).toLong()
        }

    /**
     * 压力
     */
    var pressureInt: Long? = null

    var pressure: Double? = null
        get() {
            field = if (pressureInt == null) null else pressureInt!! / 100.0
            return field
        }
        set(value) {
            field = value
            pressureInt = if (value == null) null else (value * 100.0).toLong()
        }

    /**
     * 速度
     */
    var speedInt: Long? = null

    var speed: Double? = null
        get() {
            field = if (speedInt == null) null else speedInt!! / 100.0
            return field
        }
        set(value) {
            field = value
            speedInt = if (value == null) null else (value * 100.0).toLong()
        }

    /**
     * 时间
     */
    var timeSum: Long? = null

    /**
     * //累计
     */
    var realSumInt: Long? = null

    var realSum: Double? = null
        get() {
            field = if (realSumInt == null) null else realSumInt!! / 100.0
            return field
        }
        set(value) {
            field = value
            realSumInt = if (value == null) null else (value * 100.0).toLong()
        }

    var status2: Int? = null
}